package com.jeeplus.modules.statistics.web;


import com.jeeplus.common.json.AjaxJson;

import com.jeeplus.modules.statistics.model.Employee.EmployeeEntity;
import com.jeeplus.modules.statistics.service.EmployeeService;
import com.jeeplus.modules.statistics.util.PageBean;
import com.jeeplus.modules.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
//@Api(tags = "高新统计分析-员工管理")
@RequiredArgsConstructor
@RequestMapping(value = "/employee")
public class EmployeeController {

    private final EmployeeService employeeServiceImpl;


    @PostMapping("EmployeeById")
    //@ApiOperation(value = "按id查询单个员工")
    public AjaxJson findEmployeeById(String id){
        EmployeeEntity employee = employeeServiceImpl.findEmployeeById(id);
        return AjaxJson.success().put("employee",employee);
    }

    @PostMapping("findEmployee")
    //@ApiOperation(value = "按条件查询全部员分页")
    public AjaxJson findEmployee(@RequestParam("curPage")int curPage, @RequestParam("pageSize")int pageSize,
                                 String S_name, String S_no){
        Map<String,Object> map=new HashMap<>();
        map.put("curPage",curPage);
        map.put("pageSize",pageSize);
        map.put("S_name",S_name);
        map.put("S_no",S_no);
        map.put("userid", UserUtils.getUser().getId());
        PageBean<EmployeeEntity> employee = employeeServiceImpl.findEmployee(map);
        return AjaxJson.success().put("page",employee);
    }
    @PostMapping("findRegion")
    //@ApiOperation(value = "查询全部地区")
    AjaxJson findRegion(){
        List<Map<String, Object>> region = employeeServiceImpl.findRegion();
        return AjaxJson.success().put("region",region);
    }

    @PostMapping("addEmployee")
    //@ApiOperation(value = "添加员工")
    public AjaxJson addEmployee(@RequestParam("s_no") String s_no,@RequestParam("s_sysno") String s_sysno,
                                @RequestParam("s_name") String s_name,@RequestParam("s_password") String s_password,
                                @RequestParam("s_sstar") String s_sstar,@RequestParam("s_unit") String s_unit,
                               @RequestParam("s_photoPath") String s_photoPath){


        EmployeeEntity employeeEntity=new EmployeeEntity();
        employeeEntity.setS_id(UUID.randomUUID().toString());
        employeeEntity.setS_no(s_no);
        employeeEntity.setS_sysno(s_sysno);
        employeeEntity.setS_name(s_name);
        employeeEntity.setS_password(s_password);
        employeeEntity.setS_sstar(s_sstar);
        employeeEntity.setS_unit(s_unit);
        employeeEntity.setS_photoPath(s_photoPath);


        //获取全部员工
        List<EmployeeEntity> employeeAll = employeeServiceImpl.findEmployeeAll();
        int count=0;//判断计数
        int i =0;//接收返回值
        String error="";//接收照片上传返回信息
        String error1="";//接收添加账号返回信息
        //把已有的全部员工账号循环对比需要添加的账号是否以存在
        for (EmployeeEntity entity : employeeAll) {
            if(entity.getS_no().equals(s_no)){
                count++;
            }
        }

        if(count!=0){
            error1+="账号以占用";
        }else{
             i = employeeServiceImpl.addEmployee(employeeEntity);
        }

        if (i==1){
            error1+="新增账号成功!";
        }else{
            error1+="新增账号失败!";
        }

        return AjaxJson.success(error1+error);

    }


    @PostMapping("delEmployee")
    //@ApiOperation(value = "删除员工")
    AjaxJson delEmployee(String id){
        EmployeeEntity employeeById = employeeServiceImpl.findEmployeeById(id);
        if(employeeById!=null){
            employeeServiceImpl.delEmployee(id);
            return AjaxJson.success("员工删除成功");
        }
        return AjaxJson.success("员工删除失败");
    }

    @PostMapping("updateEmployee")
    //@ApiOperation(value = "修改员工")
    AjaxJson updateEmployee( String s_id, String s_photoPath,
                             String s_no, String s_sysno,
                             String s_name, String s_password,
                             String s_sstar, String s_unit){
        String error="";
        EmployeeEntity employeeEntity=new EmployeeEntity();
        employeeEntity.setS_id(s_id);
        employeeEntity.setS_no(s_no);
        employeeEntity.setS_sysno(s_sysno);
        employeeEntity.setS_name(s_name);
        employeeEntity.setS_password(s_password);
        employeeEntity.setS_sstar(s_sstar);
        employeeEntity.setS_unit(s_unit);
        employeeEntity.setS_photoPath(s_photoPath);
        employeeServiceImpl.updateEmployee(employeeEntity);

        return AjaxJson.success("员工修改成功！"+error);
    }

}


